package com.xyc.servlet.user;

import com.xyc.mapper.UserMapper;
import com.xyc.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/myUser")
public class MyUserServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //将数据中查询到的user数据条传递属性发送到myUser.jsp
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper userMapper = ctx.getBean("userMapper", UserMapper.class);
        List<User> userList = userMapper.selectUser();


        int count = userMapper.selectUserCount();

        HttpSession session = req.getSession();
        session.setAttribute("myUserCount", count);
        session.setAttribute("userList", userList);

        //设置总条数

        //跳转用户管理界面
        req.getRequestDispatcher("user/myUser.jsp").forward(req, resp);
    }
}
